<?php

if(isset($_GET['gestion_typerole_nomSpec'])) {
	$nom = $_GET['gestion_typerole_nomSpec'];
}

 $template->setTemplateFile('ajout_typerole_form','tpl/ajout_typerole_form.tpl');
  $template->parseTemplate('ajout_typerole_form',"NOM_SPECTACLE",$nom);
 $template->setTemplateFile('show_info_gestion_prix_name','tpl/show_info_gestion_prix_name.tpl');
 $template->parseTemplate('show_info_gestion_prix_name',"NOM_SPECTACLE",$nom);

// Liste déroulante des artistes
$template->setTemplateFile('liste_artistes',NULL); 
$res = SGBD::query(
		"SELECT * FROM artiste"
		);
while($data = SGBD::fetch($res)) {
	$template->setTemplateFile('temp_liste_artiste','tpl/ajout_prix_liste_categorie.tpl');
	$template->parseTemplate('temp_liste_artiste',"OPTION_NOM_CATEGORIE",$data['pkartiste'].' - '.$data['nomartiste']. ' ' . $data['prenomartiste']);
	$template->mergeTemplates('temp_liste_artiste','liste_artistes');
}
$template->parseTemplate('ajout_typerole_form',"LISTE_DEROULANTE_NUM_ARTISTE",$template->getTemplate('liste_artistes'));

//


 if(isset($_POST['numart']) && isset($_POST['role'])) {
	$temp = explode('-',$_POST['numart']);
	$numart	= sql_protect(html_escape($temp[0]));
	$role = sql_protect(html_escape($_POST['role']));
	
	$result = SGBD::query("SELECT count(*) AS res FROM typerole WHERE nomspectacle='$nom' AND pkartiste='$numart';");
	$data = SGBD::fetch($result);
	// Insertion
	if($data['res'] == 0){
	
		$result = SGBD::query(
			"INSERT INTO typerole
			VALUES ('$nom','$numart','$role'); ");
			if(!$result) {
			echo "Erreur lors de la requete SQL";
			
			}
	}
	// Modification
	else 
	{
		$result = SGBD::query(
			"UPDATE typerole
			SET role='$role'
			WHERE pkartiste = '$numart' AND nomspectacle='$nom';");
	}
		//header("Location: index.php?gestion_prix_nomSpec=$nom&page=gestion_prix");

}
 
 
 
 
 if(isset($_GET['delete_typerole_num']) && isset($_GET['gestion_typerole_nomSpec'])) {
	$typerolenum = $_GET['delete_typerole_num'];
	$nom = sql_protect(html_escape($_GET['gestion_typerole_nomSpec']));
	$resdelet = SGBD::query(
		"DELETE FROM typerole
		WHERE ((nomspectacle = '$nom') AND (pkartiste = '$typerolenum'));" );
}
 
 
 
 
 
 
 
 
 $result = SGBD::query(
		"SELECT a.pkartiste, a.nomartiste, a.prenomartiste, t.role FROM artiste a,typerole t WHERE ((a.pkartiste=t.pkartiste) AND (t.nomspectacle='$nom'));"
		);
if(!$result)
	throw new Exception("Erreur lors de la requête SQL " . $result);	

// Extraction des donnees   
// ... et Creation d'un template vide qui va recevoir tous les spectacles recherchés
$template->setTemplateFile('show_list_gestion_salle',NULL); 

while($data = SGBD::fetch($result)){
	$template->setTemplateFile('show_infos_gestion_typerole','tpl/show_infos_gestion_typerole.tpl');
	$template->parseTemplate('show_infos_gestion_typerole',"NUM_ART",$data['pkartiste']);
	$template->parseTemplate('show_infos_gestion_typerole',"NOM_ART",$data['nomartiste']);
	$template->parseTemplate('show_infos_gestion_typerole',"PRENOM_ART",$data['prenomartiste']);
	$template->parseTemplate('show_infos_gestion_typerole',"ROLE_ART",$data['role']);
	$template->parseTemplate('show_infos_gestion_typerole',"NOMSPECT",$nom);
	$template->mergeTemplates('show_infos_gestion_typerole','show_list_gestion_salle');
}
$template->mergeTemplates('show_list_gestion_salle','ajout_typerole_form');
$template->mergeTemplates('ajout_typerole_form','show_info_gestion_prix_name');

$template->setTemplateFile('center','tpl/center.tpl');
		$template->parseTemplate('center',"CENTER_CONTENT",$template->getTemplate('show_info_gestion_prix_name'));



?>
